<?php

namespace app\controllers;
use Yii;
class UploadController extends \yii\web\Controller{
    //put your code here
    //文件上传
    public function actionUpload(){
        $model = new \app\models\Upload();
        if(Yii::$app->request->getIsPost()){
          $model->imgurl = \yii\web\UploadedFile::getInstance($model,'imgurl');
//          var_dump($model->imgurl);'
//          var_dump($_FILES);
          $filename = "upload/".date("Y-m-d")."/";
//          $name = date('H-i-s');
          $name = time().str_pad(rand(1,999),5,0,STR_PAD_RIGHT); //生成随机数
          $ext = strrchr($model->imgurl->name, '.');
          if(!file_exists($filename)){
              mkdir($filename,777);
          }
          if($model->imgurl->saveAs($filename.$name.$ext)){     //将上传的文件保存   
              
              echo "<script> alert('文件上传成功') </script>";
          }else{
               echo "<script> alert('文件上传失败') </script>";
          }
          
          }
        
        
        return $this->render('upload',['model'=>$model]);
        
    }
    
    
    
    
    
    
}
